//
//  MLAEaterBookiingVC.m
//  MagicLampApp
//
//  Created by Zheng on 2019/4/9.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAEaterBookiingVC.h"
#import "MLAEaterBookingNumView.h"
#import "MLABaseFieldModel.h"
#import "MLABaseFieldView.h"
#import "LYSDatePickerController.h"

@interface MLAEaterBookiingVC ()

@property (nonatomic, strong) MLAEaterBookingNumView *numView;


@end


@implementation MLAEaterBookiingVC {
    NSArray<MLABaseFieldModel *> *modelsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configTitle:@"预约"];
    
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, CX_GetScreenWidth(), 500);
    
    _numView = [[[NSBundle mainBundle] loadNibNamed:@"MLAEaterBookingNumView" owner:self options:nil] lastObject];
    [headerView addSubview:_numView];
    [_numView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(40);
    }];
    
    modelsArr = [self getModelsArr];

    [modelsArr enumerateObjectsUsingBlock:^(MLABaseFieldModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        MLABaseFieldView *fieldView = [[MLABaseFieldView alloc] init];
        fieldView.tag = 13000+idx;
        fieldView.model = obj;
        [headerView addSubview:fieldView];
        
        if (idx == 2) {
            @weakify(self);
            fieldView.clickActionBlock = ^{
                @strongify(self);
                [self selectTime];
            };
        }

        
        if (idx == 0) {

            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(60);
                make.left.right.equalTo(headerView);
                make.height.mas_equalTo(85);
            }];
        } else {

            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                UIView *view = [headerView viewWithTag:13000+idx-1];
                make.left.right.height.equalTo(view);
                make.top.equalTo(view.mas_bottom);
            }];
        }
    }];
    
    UIButton *commitBtn = [self getCommitButtonWithTitle:@"提交"];
    [commitBtn addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        UIView *view = [headerView viewWithTag:13000+self->modelsArr.count-1];
        make.top.equalTo(view.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(40);
    }];

    
    self.tableView.tableHeaderView = headerView;
    
}


- (NSString *)getContentWithTag:(NSInteger)tag {
    MLABaseFieldView *fieldView = (MLABaseFieldView *)[self.tableView.tableHeaderView viewWithTag:tag];
    return fieldView.inputText;
}

- (NSArray<MLABaseFieldModel *> *)getModelsArr {
    
    NSArray *infosArr = @[CX_GetFieldInfoDic(@"姓名：", @"输入姓名", @"30", @"0", @"1", @"0", @""),
                          CX_GetFieldInfoDic(@"手机号：", @"输入手机号", @"30", @"0", @"1", @"0", @""),
                          CX_GetFieldInfoDic(@"就餐时间：", @"选择就餐时间", @"30", @"0", @"0", @"1", @""),
                          CX_GetFieldInfoDic(@"备注：", @"输入备注", @"40", @"0", @"1", @"0", @"")];
    return [[MLABaseFieldModel mj_objectArrayWithKeyValuesArray:infosArr] copy];
}

#pragma mark - Actions
- (void)selectTime {
    [LYSDatePickerController alertDatePickerInWindowRootVC];
    [LYSDatePickerController customdidSelectDatePicker:^(NSDate *date) {
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSString *currentDate = [dateFormat stringFromDate:date];
        
        MLABaseFieldView *txfView = (MLABaseFieldView *)[self.tableView.tableHeaderView viewWithTag:13002];
        txfView.inputText = currentDate;
    }];

}


- (void)commitBtnAction {
    
    NSString *name = [self getContentWithTag:13000];
    if (name.length == 0) {
        [MLAHudManage showMessage:@"请输入姓名"];
        return;
    }
    
    NSString *phone = [self getContentWithTag:13001];
    if (phone.length == 0) {
        [MLAHudManage showMessage:@"请输入手机号"];
        return;
    }

    NSString *time = [self getContentWithTag:13002];
    if (time.length == 0) {
        [MLAHudManage showMessage:@"请选择就餐时间"];
        return;
    }

    NSString *content = [self getContentWithTag:13003];
    if (!content) {
        content = @"";
    }
    
    NSString *num = _numView.numLbl.text;
    
    NSDictionary *params = @{@"shop_id":_shopId,
                             @"meal_time":time,
                             @"name":name,
                             @"mobile":phone,
                             @"num":num,
                             @"content":content
                             };
    
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kShopSubscribeAdd params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [MLAHudManage showMessage:@"预约成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^{
        
    }];
    
}

@end
